package service

import (
    "context"
    "gitee.com/WisdomClassroom/CollegeStruct/glb"
    "gitee.com/WisdomClassroom/core"
    "gitee.com/WisdomClassroom/core/models"
    "gitee.com/WisdomClassroom/core/protobufs/v1/pb"
    "github.com/jinzhu/gorm"
    "time"
)

func (s *Service) SetCollegeManager(_ context.Context, in *pb.SetCollegeManagerRequest) (*pb.SetCollegeManagerResponse, error) {
    token := core.NewToken()
    err := token.UnpackToken(in.AuthToken, &core.FlagTokenCert)
    if err != nil {
        return &pb.SetCollegeManagerResponse{Status: &pb.ResponseStatus{
            Code: pb.ResponseStatusCode_NotAuth, Error: err.Error(),
        },}, nil
    }

    if token.Type == core.UserRoleTypeCodeRoot {
        userOfReq := models.UserModel{
            Model: models.Model{
                UUID: token.UUID,
            },
            Type: core.UserRoleTypeCodeRoot,
        }
        err = glb.DB.Scopes(notDeleted).Where(&userOfReq).First(&userOfReq).Error
        if err == gorm.ErrRecordNotFound {
            return &pb.SetCollegeManagerResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
                Message: "用户没有权限",
            },}, nil
        } else if err != nil {
            glb.Logger.Error(err.Error())
            return setCollegeManagerRespSvrErr, nil
        }
    } else {
        collegeMngrBindOfReq := models.CollegeManagerBindModel{
            CollegeUUID: in.WillSetCollegeUUID,
            ManagerUUID: token.UUID,
        }
        err = glb.DB.Scopes(notDeleted).Where(&collegeMngrBindOfReq).First(&collegeMngrBindOfReq).Error
        if err == gorm.ErrRecordNotFound {
            return &pb.SetCollegeManagerResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
                Message: "用户没有权限或编号不存在",
            },}, nil
        } else if err != nil {
            glb.Logger.Error(err.Error())
            return setCollegeManagerRespSvrErr, nil
        }
    }

    if len(in.WillSetCollegeUUID) != 36 {
        return &pb.SetCollegeManagerResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
            Message: "学院编号不正确",
        },}, nil
    }
    if len(in.WillSetManagerUUID) != 36 {
        return &pb.SetCollegeManagerResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
            Message: "管理员编号不正确",
        },}, nil
    }

    collegeWithUUID := models.CollegeModel{
        Model: models.Model{
            UUID: in.WillSetCollegeUUID,
        },
    }
    err = glb.DB.Scopes(notDeleted).Where(&collegeWithUUID).First(&collegeWithUUID).Error
    if err == gorm.ErrRecordNotFound {
        return &pb.SetCollegeManagerResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
            Message: "学院编号不存在",
        },}, nil
    } else if err != nil {
        glb.Logger.Error(err.Error())
        return setCollegeManagerRespSvrErr, nil
    }

    userOfManagerOfArg := models.UserModel{
        Model: models.Model{
            UUID: in.WillSetManagerUUID,
        },
    }
    err = glb.DB.Scopes(notDeleted).Where(&userOfManagerOfArg).First(&userOfManagerOfArg).Error
    if err == gorm.ErrRecordNotFound {
        return &pb.SetCollegeManagerResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
            Message: "管理员编号不存在",
        },}, nil
    } else if err != nil {
        glb.Logger.Error(err.Error())
        return setCollegeManagerRespSvrErr, nil
    }

    collegeMngrBindOfArg := models.CollegeManagerBindModel{
        CollegeUUID: in.WillSetCollegeUUID,
        ManagerUUID: in.WillSetManagerUUID,
    }
    err = glb.DB.Scopes(notDeleted).Where(&collegeMngrBindOfArg).First(&collegeMngrBindOfArg).Error
    if err == nil {
        return &pb.SetCollegeManagerResponse{Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_OtherError,
            Message: "此管理员绑定组合已存在",
        },}, nil
    } else if err != gorm.ErrRecordNotFound {
        glb.Logger.Error(err.Error())
        return setCollegeManagerRespSvrErr, nil
    }

    collegeMngrBind := models.CollegeManagerBindModel{
        Model: models.Model{
            UUID:         core.GenUUID(),
            CreationTime: time.Now().Unix(),
        },
        CollegeUUID: in.WillSetCollegeUUID,
        ManagerUUID: in.WillSetManagerUUID,
    }
    err = glb.DB.Create(&collegeMngrBind).Error
    if err != nil {
        glb.Logger.Error(err.Error())
        return setCollegeManagerRespSvrErr, nil
    }
    return &pb.SetCollegeManagerResponse{
        Status: &pb.ResponseStatus{Code: pb.ResponseStatusCode_Suc},
    }, nil
}
